﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using mki.Backup.Objects.Dialogs;

namespace mki.Backup.Objects.Editors
{
	/// <summary>
	/// Represents the FileNameUITypeEditor class
	/// </summary>
	public class FileNameUITypeEditor : UITypeEditor
	{
		#region Methods (2)

		// Public Methods (2) 

		/// <summary>
		/// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"/> method.
		/// </summary>
		/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that can be used to gain additional context information.</param>
		/// <param name="provider">An <see cref="T:System.IServiceProvider"/> that this editor can use to obtain services.</param>
		/// <param name="value">The object to edit.</param>
		/// <returns>
		/// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
		/// </returns>
		public override Object EditValue(ITypeDescriptorContext context, IServiceProvider provider, Object value)
		{
			var service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
			if (service != null)
			{
				using (var fileBrowser = new FileNameEditorForm())
				{
					fileBrowser.FileName = value.ToString();

					if (fileBrowser.ShowDialog() == DialogResult.OK)
					{
						return fileBrowser.FileName;
					}
				}
			}
			return value;
		}

		/// <summary>
		/// Gets the editor style used by the <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)"/> method.
		/// </summary>
		/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that can be used to gain additional context information.</param>
		/// <returns>
		/// A <see cref="T:System.Drawing.Design.UITypeEditorEditStyle"/> value that indicates the style of editor used by the <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)"/> method. If the <see cref="T:System.Drawing.Design.UITypeEditor"/> does not support this method, then <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"/> will return <see cref="F:System.Drawing.Design.UITypeEditorEditStyle.None"/>.
		/// </returns>
		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
		{
			return UITypeEditorEditStyle.Modal;
		}

		#endregion Methods
	}
}